package lambda;

import org.junit.Test;

import java.time.*;
import java.time.format.DateTimeFormatter;
import java.util.concurrent.TimeUnit;

/**
 * @author : 李红磊
 * @version :1.0
 * @date : 2021/10/9 20:27
 * @description : 时间API
 */
public class Demo3 {


    @Test
    public void test1() {
        /*
         * Instant 时间戳（以UNIX元年1970-1-1 00:00:00到某个时间之间的毫秒值
         * */

        //以UNIX元年1970-1-1 00:00:00到现在的毫秒值
        Instant now = Instant.now();//UTC时间
        System.out.println(now);//加偏移量

        OffsetDateTime time = now.atOffset(ZoneOffset.ofHours(8));
        System.out.println(time);


    }

    @Test
    public void test2() {
        LocalDateTime localDateTime = LocalDateTime.now();
        System.out.println(localDateTime);


    }


    @Test
    public void test3() {
        /*
         * Duration:计算两个时间之间的间隔
         *
         * */

        Instant now = Instant.now();

        try {
            TimeUnit.SECONDS.sleep(2);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        Instant now2 = Instant.now();

        Duration duration = Duration.between(now, now2);
        System.out.println(duration.toMillis());
        System.out.println(duration);

    }


    @Test
    public void test4() {
        /*
         * Period:获取两个日期的间隔
         *
         * */
        LocalDate date1 = LocalDate.of(1999, 11, 29);

        LocalDate date2 = LocalDate.now();

        Period period = Period.between(date1, date2);
        System.out.println("距离我出生已经过去了" + period.getYears() + "年");

    }


    @Test
    public void test5() {
        /**
         * DateTimeFormatter:时间日期格式化
         */

        DateTimeFormatter isoDate = DateTimeFormatter.ISO_DATE;
        LocalDateTime time = LocalDateTime.now();
        String res = time.format(isoDate);
        System.out.println(res);


    }

}
